type Cloneable = object | any[] | Date | RegExp | null | undefined;

// 深拷贝
export const cloneDeep = <T extends Cloneable>(value: T): T => {
    if (value === null || typeof value !== 'object') {
        return value as T;
    }

    if (value instanceof Date) {
        return new Date(value) as T;
    }
    if (value instanceof RegExp) {
        return new RegExp(value) as T;
    }

    if (Array.isArray(value)) {
        const result = [] as any[];
        for (const item of value) {
            result.push(cloneDeep(item));
        }
        return result as T;
    }

    const result = {} as AnyObject;
    for (const key of Object.keys(value)) {
        result[key] = cloneDeep(value[key]);
    }
    return result as T;
};
